#include"udp_server.hpp"
#include<memory>
#include<string>
#include<cstring>

using namespace ns_server;
using namespace std;

static void usage(string proc)
{
    std::cout<<"Usage:\n\t"<<proc<<"proc\n"<<std::endl;
}

std::string transaction(std::string request)
{
    std::string reslut;
    char c;
    for(auto &r:request)
    {
        if(islower(r)) 
        {
        c=toupper(r);
        reslut.push_back(c);
        }
        else
        {
            reslut.push_back(r);
        }
    }
    return reslut;
}
static bool isPass(const std::string& command)
{
    bool pass=true;
    auto pos=command.find("rm");
    if(pos!=std::string::npos) pass=false;
    return pass;
}
std::string excuteCommand(std::string command)
{
    //1.安全检查
    if(!isPass(command)) return "None";
   
    //2.业务逻辑
    FILE* fp=popen(command.c_str(),"r");
    if(fp==nullptr) return "None";
    char line[1024];
    std::string reslut;
    while(fgets(line,sizeof(line),fp)!=NULL)
    {
        reslut+=line;
    }
    pclose(fp);
    return reslut;

}
int main(int argc,char* argv[])
{
    if(argc!=2)
    {
        usage(argv[0]);
        exit(USAGE_ERR);
    }
    uint16_t port=atoi(argv[1]);
    unique_ptr<UdpServer> usvr(new UdpServer(excuteCommand,port));
    usvr->InitServer();
    usvr->Start();
    return 0;


}
